package com.flyme.module.base.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysActivityrecord;
import com.flyme.module.base.client.entity.SysStudyconfig;
import com.flyme.module.base.client.entity.SysUsercertificate;
import com.flyme.module.base.provider.mapper.SysActivityrecordMapper;
import com.flyme.module.base.provider.service.SysActivityrecordService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysActivityrecordServiceImpl extends BaseServiceImpl<SysActivityrecordMapper, SysActivityrecord> implements SysActivityrecordService {

    @Override
    public Boolean edit(SysActivityrecord activityrecord) {
        return saveOrUpdate(activityrecord);
    }

    @Override
    public List<EntityMap> getActivityList(Long certifiId, String startTime, String endTime) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>();
        queryWrapper.select("*");
        queryWrapper.eq("usercertificateId", certifiId);
        queryWrapper.eq("activityType", 1);
        queryWrapper.between("createTime", startTime, endTime);
        return listEntityMaps(queryWrapper);
    }
    @Override
    public EntityMap checkActivity(SysUsercertificate usercertificate, SysStudyconfig studyconfig){
        EntityMap checkResult = new EntityMap();
        Date date = DateUtil.parse(usercertificate.getValidity());
        CriteriaQuery cq = new CriteriaQuery<SysActivityrecord>();
        cq.select("IFNULL(SUM(period), 0) sumPeriod");
        cq.eq("activityType", 1);
        cq.eq("usercertificateId", usercertificate.getId());
        cq.ge("createTime", DateUtil.year(date) + "-01-01");
        cq.le("createTime", usercertificate.getValidity());
        EntityMap result = getEntityMap(cq);
        String periodString = result.get("sumPeriod").toString();
        //1校验活动学时是否已经满了
        Integer userSumPeriod = Integer.valueOf(periodString);//用户当前总活动学时
        Integer maxPeriod = studyconfig.getOfflinePeriod();//最大活动学时
        //用户该证书活动学时大于最大活动学时
        if(userSumPeriod >= maxPeriod){
            checkResult.put("checkType", 3);
        }
        if(userSumPeriod < maxPeriod){
            //用户单个增加的活动学时大于最大活动学时与当前活动学时之差
            if(studyconfig.getOncePeriod() > (maxPeriod - userSumPeriod)){
                checkResult.put("checkType", 2);
            }else{
                checkResult.put("checkType", 1);
            }
        }
        return checkResult;
    }

    public static void main(String[] args) {
        Date date = DateUtil.parse("2026-01-01");
        Date newDate = DateUtil.offsetMonth(date, 12);
        String date2 = DateUtil.formatDate(newDate);
        String year = DateUtil.year(DateUtil.date()) + "";
        String open = DateUtil.year(DateUtil.date()) + "-01-01";
        System.out.println(open);
    }
}
